﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Configuration;
using System.Reflection;

using Microsoft.Practices.EnterpriseLibrary.Caching.Expirations;
using Microsoft.Practices.EnterpriseLibrary.Caching;

namespace Unis.ORM
{
    public class MbOrmCacheCofigurationHandler : IConfigurationSectionHandler
    {
        // Methods
        private void GetICacheItemExpiration(string interval, string assemblyName, string className, ref ICacheItemExpiration[] ie)
        {
            switch (className)
            {
                case "Microsoft.ApplicationBlocks.Cache.ExpirationsImplementations.SlidingTime":
                    {
                        double num = double.Parse(interval);
                        ie = new SlidingTime[] { new SlidingTime(TimeSpan.FromSeconds(num)) };
                        return;
                    }
                case "Microsoft.ApplicationBlocks.Cache.ExpirationsImplementations.AbsoluteTime":
                    {
                        double num2 = double.Parse(interval);
                        DateTime absoluteTime = DateTime.Now.AddSeconds(num2);
                        ie = new AbsoluteTime[] { new AbsoluteTime(absoluteTime) };
                        return;
                    }
                case "Microsoft.ApplicationBlocks.Cache.ExpirationsImplementations.FileDependency":
                    ie = new FileDependency[] { new FileDependency(interval) };
                    return;

                case "Microsoft.ApplicationBlocks.Cache.ExpirationsImplementations.ExtendedFormatTime":
                    ie = new ExtendedFormatTime[] { new ExtendedFormatTime(interval) };
                    return;
            }
            ICacheItemExpiration expiration = Assembly.Load(assemblyName).CreateInstance(className) as ICacheItemExpiration;
            ie = Array.CreateInstance(expiration.GetType(), 1) as ICacheItemExpiration[];
            ie[0] = expiration;
        }

        object IConfigurationSectionHandler.Create(object parent, object context, XmlNode section)
        {
            ICacheItemExpiration[] ie = null;
            object obj2;
            try
            {
                if (object.Equals(section, null))
                {
                    throw new ArgumentNullException("section");
                }
                XmlNode objA = section.SelectSingleNode("MB.ExpirationPolicy");
                if (!object.Equals(objA, null))
                {
                    string interval = objA.Attributes["ExpirationCheckInterval"].Value;
                    string assemblyName = objA.Attributes["AssemblyName"].Value;
                    string className = objA.Attributes["ClassName"].Value;
                    this.GetICacheItemExpiration(interval, assemblyName, className, ref ie);
                }
                obj2 = ie;
            }
            catch (Exception exception)
            {
                throw exception;
            }
            return obj2;
        }
    }
}
